﻿using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Terraria.GameContent.UI.Elements;
using Terraria.Localization;
using Terraria.ModLoader.UI;

namespace Terraria.ModLoader
{
	// Token: 0x02000197 RID: 407
	internal static class Interface
	{
		// Token: 0x060010DC RID: 4316 RVA: 0x004050EC File Offset: 0x004032EC
		internal static void AddMenuButtons(Main main, int selectedMenu, string[] buttonNames, float[] buttonScales, ref int offY, ref int spacing, ref int buttonIndex, ref int numButtons)
		{
			buttonNames[buttonIndex] = Language.GetTextValue("tModLoader.MenuMods");
			if (selectedMenu == buttonIndex)
			{
				Main.PlaySound(10, -1, -1, 1, 1f, 0f);
				Main.menuMode = 10000;
			}
			buttonIndex++;
			numButtons++;
			buttonNames[buttonIndex] = Language.GetTextValue("tModLoader.MenuModSources");
			if (selectedMenu == buttonIndex)
			{
				Main.PlaySound(10, -1, -1, 1, 1f, 0f);
				Main.menuMode = 10001;
			}
			buttonIndex++;
			numButtons++;
			buttonNames[buttonIndex] = Language.GetTextValue("tModLoader.MenuModBrowser");
			if (selectedMenu == buttonIndex)
			{
				Main.PlaySound(10, -1, -1, 1, 1f, 0f);
				Main.menuMode = 10007;
			}
			buttonIndex++;
			numButtons++;
			offY = 220;
			for (int i = 0; i < numButtons; i++)
			{
				buttonScales[i] = 0.82f;
			}
			spacing = 45;
		}

		// Token: 0x060010DD RID: 4317 RVA: 0x004051E8 File Offset: 0x004033E8
		internal static void ResetData()
		{
			UIList modList = Interface.modBrowser.modList;
			if (modList != null)
			{
				modList.Clear();
			}
			Interface.modBrowser.sortMode = ModBrowserSortMode.RecentlyUpdated;
			Interface.modBrowser.updateFilterMode = UpdateFilter.Available;
			Interface.modBrowser.searchFilterMode = SearchFilter.Name;
			Interface.modBrowser.modSideFilterMode = ModSideFilter.All;
			UICycleImage searchFilterToggle = Interface.modBrowser.SearchFilterToggle;
			if (searchFilterToggle != null)
			{
				searchFilterToggle.setCurrentState((int)Interface.modBrowser.searchFilterMode);
			}
			if (Interface.modBrowser._categoryButtons.Count == 3)
			{
				Interface.modBrowser._categoryButtons[0].setCurrentState((int)Interface.modBrowser.sortMode);
				Interface.modBrowser._categoryButtons[1].setCurrentState((int)Interface.modBrowser.updateFilterMode);
				Interface.modBrowser._categoryButtons[2].setCurrentState((int)Interface.modBrowser.modSideFilterMode);
			}
			Interface.modBrowser.loading = false;
			ModLoader.modsDirCache.Clear();
			GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
		}

		// Token: 0x060010DE RID: 4318 RVA: 0x004052E4 File Offset: 0x004034E4
		internal static void ModLoaderMenus(Main main, int selectedMenu, string[] buttonNames, float[] buttonScales, int[] buttonVerticalSpacing, ref int offY, ref int spacing, ref int numButtons, ref bool backButtonDown)
		{
			if (Main.menuMode == 10000)
			{
				Main.MenuUI.SetState(Interface.modsMenu);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10001)
			{
				Main.MenuUI.SetState(Interface.modSources);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10002)
			{
				Main.MenuUI.SetState(Interface.loadMods);
				Main.menuMode = 888;
				ModLoader.Load();
				return;
			}
			if (Main.menuMode == 10003)
			{
				Main.MenuUI.SetState(Interface.buildMod);
				Main.menuMode = 888;
				ModLoader.BuildMod();
				return;
			}
			if (Main.menuMode == 10004)
			{
				Main.MenuUI.SetState(Interface.buildMod);
				Main.menuMode = 888;
				ModLoader.BuildAllMods();
				return;
			}
			if (Main.menuMode == 10005)
			{
				Main.MenuUI.SetState(Interface.errorMessage);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10006)
			{
				ModLoader.Reload();
				return;
			}
			if (Main.menuMode == 10007)
			{
				Main.MenuUI.SetState(Interface.modBrowser);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10008)
			{
				Main.MenuUI.SetState(Interface.modInfo);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10009)
			{
				Main.MenuUI.SetState(Interface.downloadMod);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10020)
			{
				Main.menuMode = 888;
				Main.MenuUI.SetState(Interface.downloadMods);
				return;
			}
			if (Main.menuMode == 10011)
			{
				Main.MenuUI.SetState(Interface.managePublished);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10012)
			{
				Main.MenuUI.SetState(Interface.updateMessage);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10013)
			{
				Main.MenuUI.SetState(Interface.infoMessage);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10014)
			{
				Main.MenuUI.SetState(Interface.advancedInfoMessage);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10015)
			{
				Main.MenuUI.SetState(Interface.enterPassphraseMenu);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10018)
			{
				Main.MenuUI.SetState(Interface.enterSteamIDMenu);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10016)
			{
				Main.MenuUI.SetState(Interface.modPacksMenu);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10019)
			{
				Main.MenuUI.SetState(Interface.extractMod);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10021)
			{
				Main.MenuUI.SetState(Interface.uploadMod);
				Main.menuMode = 888;
				return;
			}
			if (Main.menuMode == 10017)
			{
				offY = 210;
				spacing = 42;
				numButtons = 7;
				buttonVerticalSpacing[numButtons - 1] = 18;
				for (int i = 0; i < numButtons; i++)
				{
					buttonScales[i] = 0.75f;
				}
				int num = 0;
				buttonNames[num] = (ModNet.downloadModsFromServers ? Language.GetTextValue("tModLoader.DownloadFromServersYes") : Language.GetTextValue("tModLoader.DownloadFromServersNo"));
				if (selectedMenu == num)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					ModNet.downloadModsFromServers = !ModNet.downloadModsFromServers;
				}
				num++;
				buttonNames[num] = (ModNet.onlyDownloadSignedMods ? Language.GetTextValue("tModLoader.DownloadSignedYes") : Language.GetTextValue("tModLoader.DownloadSignedNo"));
				if (selectedMenu == num)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					ModNet.onlyDownloadSignedMods = !ModNet.onlyDownloadSignedMods;
				}
				num++;
				buttonNames[num] = ((ModLoader.musicStreamMode == 0) ? Language.GetTextValue("tModLoader.MusicStreamModeConvert") : Language.GetTextValue("tModLoader.MusicStreamModeStream"));
				if (selectedMenu == num)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					ModLoader.musicStreamMode = (ModLoader.musicStreamMode + 1) % 2;
				}
				num++;
				buttonNames[num] = (ModLoader.alwaysLogExceptions ? Language.GetTextValue("tModLoader.AlwaysLogExceptionsYes") : Language.GetTextValue("tModLoader.AlwaysLogExceptionsNo"));
				if (selectedMenu == num)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					ModLoader.alwaysLogExceptions = !ModLoader.alwaysLogExceptions;
				}
				num++;
				buttonNames[num] = (Main.UseExperimentalFeatures ? Language.GetTextValue("tModLoader.ExperimentalFeaturesYes") : Language.GetTextValue("tModLoader.ExperimentalFeaturesNo"));
				if (selectedMenu == num)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					Main.UseExperimentalFeatures = !Main.UseExperimentalFeatures;
				}
				num++;
				buttonNames[num] = Language.GetTextValue("tModLoader.ClearMBCredentials");
				if (selectedMenu == num)
				{
					Main.PlaySound(12, -1, -1, 1, 1f, 0f);
					ModLoader.modBrowserPassphrase = "";
					ModLoader.SteamID64 = "";
				}
				num++;
				buttonNames[num] = Lang.menu[5].Value;
				if (selectedMenu == num | backButtonDown)
				{
					backButtonDown = false;
					Main.menuMode = 11;
					Main.PlaySound(11, -1, -1, 1, 1f, 0f);
				}
			}
		}

		// Token: 0x060010DF RID: 4319 RVA: 0x00405814 File Offset: 0x00403A14
		internal static void ServerModMenu()
		{
			bool flag = false;
			while (!flag)
			{
				Console.WriteLine("Terraria Server " + Main.versionNumber2 + " - " + ModLoader.versionedName);
				Console.WriteLine();
				LocalMod[] array = ModLoader.FindMods();
				for (int i = 0; i < array.Length; i++)
				{
					Console.Write(i + 1 + "\t\t" + array[i].DisplayName);
					Console.WriteLine(" (" + (ModLoader.IsEnabled(array[i].Name) ? "enabled" : "disabled") + ")");
				}
				if (array.Length == 0)
				{
					Console.ForegroundColor = ConsoleColor.Yellow;
					Console.WriteLine(string.Format("No mods were found in: \"{0}\"\nIf you are running a dedicated server, you may wish to use the 'modpath' command line switch or server config setting to specify a custom mods directory.\n", ModLoader.ModPath));
					Console.ResetColor();
				}
				Console.WriteLine("e\t\tEnable All");
				Console.WriteLine("d\t\tDisable All");
				Console.WriteLine("r\t\tReload and return to world menu");
				Console.WriteLine("Type a number to switch between enabled/disabled");
				Console.WriteLine();
				Console.WriteLine("Type a command: ");
				string text = Console.ReadLine();
				if (text == null)
				{
					text = "";
				}
				text = text.ToLower();
				Console.Clear();
				int num;
				if (text == "e")
				{
					foreach (LocalMod localMod in array)
					{
						localMod.Enabled = true;
					}
				}
				else if (text == "d")
				{
					foreach (LocalMod localMod2 in array)
					{
						localMod2.Enabled = false;
					}
				}
				else if (text == "r")
				{
					Console.WriteLine("Unloading mods...");
					ModLoader.Unload();
					ModLoader.do_Load(null);
					flag = true;
				}
				else if (int.TryParse(text, out num) && num > 0 && num <= array.Length)
				{
					LocalMod localMod3 = array[num - 1];
					localMod3.Enabled = !localMod3.Enabled;
				}
			}
		}

		// Token: 0x060010E0 RID: 4320 RVA: 0x004059E4 File Offset: 0x00403BE4
		internal static void ServerModBrowserMenu()
		{
			bool flag = false;
			Console.Clear();
			while (!flag)
			{
				Console.WriteLine();
				Console.WriteLine("b\t\tReturn to world menu");
				Console.WriteLine();
				Console.WriteLine("Type an exact ModName to download: ");
				string text = Console.ReadLine();
				if (text == null)
				{
					text = "";
				}
				if (text == "b" || text == "B")
				{
					flag = true;
				}
				else
				{
					string text2 = text;
					try
					{
						ServicePointManager.ServerCertificateValidationCallback = ((object o, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true);
						using (WebClient webClient = new WebClient())
						{
							string text3 = webClient.DownloadString(string.Format("http://javid.ddns.net/tModLoader/tools/querymoddownloadurl.php?modname={0}", text2));
							if (text3.StartsWith("Failed"))
							{
								Console.WriteLine(text3);
							}
							else
							{
								string text4 = ModLoader.ModPath + Path.DirectorySeparatorChar.ToString() + "temporaryDownload.tmod";
								webClient.DownloadFile(text3, text4);
								File.Copy(text4, ModLoader.ModPath + Path.DirectorySeparatorChar.ToString() + text3.Substring(text3.LastIndexOf("/")), true);
								File.Delete(text4);
							}
							while (Console.KeyAvailable)
							{
								Console.ReadKey(true);
							}
						}
					}
					catch (Exception ex)
					{
						Console.WriteLine("Error: Could not download " + text2 + " -- " + ex.ToString());
					}
				}
			}
			Console.Clear();
		}

		// Token: 0x0400135E RID: 4958
		internal const int modsMenuID = 10000;

		// Token: 0x0400135F RID: 4959
		internal const int modSourcesID = 10001;

		// Token: 0x04001360 RID: 4960
		internal const int loadModsID = 10002;

		// Token: 0x04001361 RID: 4961
		internal const int buildModID = 10003;

		// Token: 0x04001362 RID: 4962
		internal const int buildAllModsID = 10004;

		// Token: 0x04001363 RID: 4963
		internal const int errorMessageID = 10005;

		// Token: 0x04001364 RID: 4964
		internal const int reloadModsID = 10006;

		// Token: 0x04001365 RID: 4965
		internal const int modBrowserID = 10007;

		// Token: 0x04001366 RID: 4966
		internal const int modInfoID = 10008;

		// Token: 0x04001367 RID: 4967
		internal const int downloadModID = 10009;

		// Token: 0x04001368 RID: 4968
		internal const int managePublishedID = 10011;

		// Token: 0x04001369 RID: 4969
		internal const int updateMessageID = 10012;

		// Token: 0x0400136A RID: 4970
		internal const int infoMessageID = 10013;

		// Token: 0x0400136B RID: 4971
		internal const int advancedInfoMessageID = 10014;

		// Token: 0x0400136C RID: 4972
		internal const int enterPassphraseMenuID = 10015;

		// Token: 0x0400136D RID: 4973
		internal const int modPacksMenuID = 10016;

		// Token: 0x0400136E RID: 4974
		internal const int tModLoaderSettingsID = 10017;

		// Token: 0x0400136F RID: 4975
		internal const int enterSteamIDMenuID = 10018;

		// Token: 0x04001370 RID: 4976
		internal const int extractModID = 10019;

		// Token: 0x04001371 RID: 4977
		internal const int downloadModsID = 10020;

		// Token: 0x04001372 RID: 4978
		internal const int uploadModID = 10021;

		// Token: 0x04001373 RID: 4979
		internal static UIMods modsMenu = new UIMods();

		// Token: 0x04001374 RID: 4980
		internal static UILoadMods loadMods = new UILoadMods();

		// Token: 0x04001375 RID: 4981
		private static UIModSources modSources = new UIModSources();

		// Token: 0x04001376 RID: 4982
		internal static UIBuildMod buildMod = new UIBuildMod();

		// Token: 0x04001377 RID: 4983
		internal static UIErrorMessage errorMessage = new UIErrorMessage();

		// Token: 0x04001378 RID: 4984
		internal static UIModBrowser modBrowser = new UIModBrowser();

		// Token: 0x04001379 RID: 4985
		internal static UIModInfo modInfo = new UIModInfo();

		// Token: 0x0400137A RID: 4986
		internal static UIDownloadMod downloadMod = new UIDownloadMod();

		// Token: 0x0400137B RID: 4987
		internal static UIManagePublished managePublished = new UIManagePublished();

		// Token: 0x0400137C RID: 4988
		internal static UIUpdateMessage updateMessage = new UIUpdateMessage();

		// Token: 0x0400137D RID: 4989
		internal static UIInfoMessage infoMessage = new UIInfoMessage();

		// Token: 0x0400137E RID: 4990
		internal static UIAdvancedInfoMessage advancedInfoMessage = new UIAdvancedInfoMessage();

		// Token: 0x0400137F RID: 4991
		internal static UIEnterPassphraseMenu enterPassphraseMenu = new UIEnterPassphraseMenu();

		// Token: 0x04001380 RID: 4992
		internal static UIModPacks modPacksMenu = new UIModPacks();

		// Token: 0x04001381 RID: 4993
		internal static UIEnterSteamIDMenu enterSteamIDMenu = new UIEnterSteamIDMenu();

		// Token: 0x04001382 RID: 4994
		internal static UIExtractMod extractMod = new UIExtractMod();

		// Token: 0x04001383 RID: 4995
		internal static UIDownloadMods downloadMods = new UIDownloadMods();

		// Token: 0x04001384 RID: 4996
		internal static UIUploadMod uploadMod = new UIUploadMod();
	}
}
